# function to compute baseline distribution conditional on individual beliefs/traits
base_fig <- function(item, xlabel){ 
  base_res <- as.factor(data[data$treat_intl == 0, item])
  base_tbl <- table(base_res)
  barplot(base_tbl,
          ylab="count",
          xlab=xlabel)
}

base_fig_cond <- function(item, cond, label, legend=F){
  base_tbl <- matrix(0, nrow=2, ncol=6)
  rownames(base_tbl) <- c("HI", "LO")
  colnames(base_tbl) <- seq(1, 6)
  data_hi <- subset(data, data[,cond] == 1)
  data_lo <- subset(data, data[,cond] == 0)
  base_tbl[1,] <- table(data_hi[data$treat_intl == 0, item])
  base_tbl[2,] <- table(data_lo[data$treat_intl == 0, item])
  
  if (legend){ 
    barplot(base_tbl, beside=T,
            ylab="count", xlab=label,
            legend.text = rownames(base_tbl),
            ylim=c(0, 200))
  }else{
    barplot(base_tbl, beside=T,
            ylab="count", xlab=label,
            ylim=c(0, 200))
  }
}

base_fig_ldp <- function(item, cond, label, legend=F){
  base_tbl <- matrix(0, nrow=2, ncol=6)
  rownames(base_tbl) <- c("LDP", "Others")
  colnames(base_tbl) <- seq(1, 6)
  data_hi <- subset(data, data[,cond] == 1)
  data_lo <- subset(data, data[,cond] == 0)
  base_tbl[1,] <- table(data_hi[data$treat_intl == 0, item])
  base_tbl[2,] <- table(data_lo[data$treat_intl == 0, item])
  
  if (legend){ 
    barplot(base_tbl, beside=T,
            ylab="count", xlab=label,
            legend.text = rownames(base_tbl),
            ylim=c(0, 200))
  }else{
    barplot(base_tbl, beside=T,
            ylab="count", xlab=label,
            ylim=c(0, 200))
  }
}
